<?php
/**
 * golangshop商城系统 - 团队十年电商经验汇集巨献!
 * =========================================================
 * Copy right 2019-2029 杭州瑞璞玉数字科技有限公司, 保留所有权利。
 * ----------------------------------------------
 * 官方网址: https://www.golangshop.com
 * =========================================================
 */

namespace addon\pointexchange\model;

use app\model\BaseModel;
use app\model\express\Config as ExpressConfig;
use app\model\express\Express;
use app\model\express\Local;
use app\model\goods\Goods;
use app\model\member\Member;
use app\model\member\MemberAccount;
use app\model\member\MemberAddress;
use app\model\order\Config;
use app\model\order\LocalOrder;
use app\model\order\Order as CommonOrder;
use app\model\order\OrderCreateTool;
use app\model\order\StoreOrder;
use app\model\store\Store;
use app\model\system\Cron;
use app\model\system\Pay;
use think\facade\Cache;

/**
 * 积分兑换
 */
class OrderCreate extends BaseModel
{
    use OrderCreateTool;
    private $error = 0;  //是否有错误
    private $error_msg = '';  //错误描述

    /**
     * 创建订单
     * @param $data
     */
    public function create($data)
    {

        $calculate_data = $this->calculate($data);//计算并查询套餐信息

        if (isset($calculate_data[ 'code' ]) && $calculate_data[ 'code' ] < 0)
            return $calculate_data;

        if ($this->error > 0) {
            return $this->error([ 'error_code' => $this->error ], $this->error_msg);
        }

        if (!empty($calculate_data[ 'invoice_type' ])) {
            if ($calculate_data[ 'invoice_type' ] == 1 && $calculate_data[ 'invoice_full_address' ] == "") {
                //物流,同城
                if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "express" || $calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "local") {
                    $calculate_data[ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
                    $calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $calculate_data[ 'member_address' ][ 'full_address' ] . $calculate_data[ 'member_address' ][ 'address' ];
                }
                //门店
                if ($calculate_data[ 'shop_goods_list' ][ 'delivery' ][ 'delivery_type' ] == "store") {
                    $delivery_store_info = json_decode($calculate_data[ 'shop_goods_list' ][ 'delivery_store_info' ], true);
                    $calculate_data[ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
                    $calculate_data[ 'shop_goods_list' ][ 'invoice_full_address' ] = $delivery_store_info[ 'full_address' ];
                }
            }
        }

        $pay_model = new Pay();
        $out_trade_no = $pay_model->createOutTradeNo($data[ 'member_id' ]);
        model("promotion_exchange_order")->startTrans();
        try {
            $order_no = $this->createOrderNo($calculate_data[ "site_id" ], $data[ 'member_id' ]);
            $exchange_info = $calculate_data[ "exchange_info" ] ?? [];

            $delivery_type = $data[ 'delivery' ][ 'delivery_type' ] ?? '';
            $site_id = $data[ 'site_id' ];
            $express_type_list = ( new \app\model\express\Config() )->getExpressTypeList($site_id);
            $delivery_type_name = $express_type_list[ $delivery_type ] ?? '';

            $order_id = 0;
            $order_data = array (
                "order_no" => $order_no,
                "member_id" => $data[ "member_id" ],
                "out_trade_no" => $out_trade_no,
                "point" => $calculate_data[ "point" ],
                "exchange_price" => $exchange_info[ "price" ],
                "delivery_price" => $calculate_data[ "delivery_money" ],
                "price" => $calculate_data[ 'exchange_info' ][ "price" ],
                "order_money" => $calculate_data[ "order_money" ],
                "create_time" => time(),
                "exchange_id" => $exchange_info[ "id" ],
                "exchange_goods_id" => $exchange_info[ "exchange_goods_id" ],
                "exchange_name" => $exchange_info[ "name" ],
                "exchange_image" => $exchange_info[ "image" ],
                "num" => $calculate_data[ "num" ],
                "order_status" => 0,
                "type" => $exchange_info[ "type" ],
                "type_name" => $exchange_info[ "type_name" ],
                'name' => $calculate_data[ 'member_address' ][ 'name' ] ?? '',
                'mobile' => $calculate_data[ 'member_address' ][ 'mobile' ] ?? '',
                'telephone' => $calculate_data[ 'member_address' ][ 'telephone' ] ?? '',
                'province_id' => $calculate_data[ 'member_address' ][ 'province_id' ] ?? '',
                'city_id' => $calculate_data[ 'member_address' ][ 'city_id' ] ?? '',
                'district_id' => $calculate_data[ 'member_address' ][ 'district_id' ] ?? '',
                'community_id' => $calculate_data[ 'member_address' ][ 'community_id' ] ?? '',
                'address' => $calculate_data[ 'member_address' ][ 'address' ] ?? '',
                'full_address' => $calculate_data[ 'member_address' ][ 'full_address' ] ?? '',
                'longitude' => $calculate_data[ 'member_address' ][ 'longitude' ] ?? '',
                'latitude' => $calculate_data[ 'member_address' ][ 'latitude' ] ?? '',
                'order_from' => $data[ 'order_from' ],
                'order_from_name' => $data[ 'order_from_name' ],
                "buyer_message" => $calculate_data[ "buyer_message" ],
                "type_id" => $exchange_info[ "type_id" ],
                "balance" => $calculate_data[ "balance" ],
                "site_id" => $calculate_data[ "site_id" ],
                'order_id' => $order_id,
                'delivery_type' => $delivery_type,
                'delivery_type_name' => $delivery_type_name,
                'delivery_status' => $calculate_data[ "delivery_status" ] ?? 0,
                'delivery_status_name' => $calculate_data[ "delivery_status_name" ] ?? '',

                'delivery_store_id' => $data[ 'delivery' ][ 'store_id' ] ?? 0,
                "delivery_store_name" => $calculate_data[ "delivery_store_name" ] ?? '',
                "delivery_store_info" => $calculate_data[ "delivery_store_info" ] ?? '',
                'buyer_ask_delivery_time' => $calculate_data[ 'buyer_ask_delivery_time' ] ?? '',//定时达
            );

            $order_id = model("promotion_exchange_order")->add($order_data);
            //判断库存
            $exchange_model = new Exchange();

            //减去套餐的库存
            $exchange_result = $exchange_model->decStock([ "id" => $exchange_info[ "id" ], "num" => $calculate_data[ "num" ] ]);
            if ($exchange_result[ 'code' ] < 0) {
                model("promotion_exchange_order")->rollback();
                return $exchange_result;
            }

            switch ( $exchange_info[ "type" ] ) {//兑换类型
                case "1"://商品
                    //库存变化
                    $result = $this->success();
                    $goods_info = $calculate_data[ 'goods_info' ];
                    $stock_result = $this->skuDecStock($goods_info, $order_data[ 'store_id' ] ?? 0);
                    if ($stock_result[ 'code' ] != 0) {
                        model('promotion_exchange_order')->rollback();
                        return $stock_result;
                    }
                    break;
                case "2"://优惠券
                    $result = $this->success();
                    break;
                case "3":
                default:
                    $result = $this->success();
                    break;
            }

            if ($result[ "code" ] < 0) {
                model("promotion_exchange_order")->rollback();
                return $result;
            }

            //扣除积分
            $member_account_model = new MemberAccount();
            $member_account_result = $member_account_model->addMemberAccount($exchange_info[ 'site_id' ], $data[ "member_id" ], "point", -$calculate_data[ "point" ], "pointexchange", $order_id, "积分兑换扣除");
            if ($member_account_result[ "code" ] < 0) {
                model("promotion_exchange_order")->rollback();
                return $member_account_result;
            }

            // 积分兑换订单生成后操作
            $result_list = event("PointExchangeOrderCreate", [ 'order_id' => $order_id ]);

            if (!empty($result_list)) {
                foreach ($result_list as $k => $v) {
                    if (!empty($v) && $v[ "code" ] < 0) {
                        model("promotion_exchange_order")->rollback();
                        return $v;
                    }
                }
            }

            //生成整体支付单据
            $pay_model = new Pay();
            $pay_model->addPay($calculate_data[ "site_id" ], $out_trade_no, "POINT", $calculate_data[ "order_name" ], $calculate_data[ "order_name" ], $calculate_data[ "order_money" ], '', 'PointexchangeOrderPayNotify', '');
            model("promotion_exchange_order")->commit();
            return $this->success($out_trade_no);

        } catch (\Exception $e) {
            model("promotion_exchange_order")->rollback();
            return $this->error('', $e->getMessage() . $e->getFile() . $e->getLine());
        }
    }

    /**
     * 待支付订单
     * @param $data
     */
    public function payment($data)
    {
        $calculate_data = $this->calculate($data);//计算并查询套餐信息
        if (isset($calculate_data[ 'code' ]) && $calculate_data[ 'code' ] < 0)
            return $calculate_data;

        $express_type = [];
        if ($calculate_data[ 'exchange_info' ][ 'type' ] == 1 && $calculate_data[ 'is_virtual' ] == 0) {
//            if (!empty($calculate_data[ 'member_address' ])) {
            //2. 查询店铺配送方式（1. 物流  2. 自提  3. 外卖）
            if ($calculate_data[ "express_config" ][ "is_use" ] == 1) {
                $title = $calculate_data[ "express_config" ][ 'value' ][ 'express_name' ];
                if ($title == "") {
                    $title = Express::express_type[ "express" ][ "title" ];
                }
                $express_type[] = [ "title" => $title, "name" => "express" ];
            }
            //查询店铺是否开启门店自提
            if ($calculate_data[ "store_config" ][ "is_use" ] == 1) {
                //根据坐标查询门店
                $store_model = new Store();
                $store_condition = array (
                    [ 'site_id', '=', $data[ 'site_id' ] ],
                    [ 'is_pickup', '=', 1 ],
                    [ 'status', '=', 1 ],
                    [ 'is_frozen', '=', 0 ],
                );
                $latlng = array (
                    'lat' => $data[ 'latitude' ],
                    'lng' => $data[ 'longitude' ],
                );
                $store_list_result = $store_model->getLocationStoreList($store_condition, '*', $latlng);
                $store_list = $store_list_result[ "data" ];
                $title = $calculate_data[ "store_config" ][ 'value' ][ 'store_name' ];
                if ($title == "") {
                    $title = Express::express_type[ "store" ][ "title" ];
                }
                $express_type[] = [ "title" => $title, "name" => "store", "store_list" => $store_list ];
            }
            //查询店铺是否开启外卖配送
            if ($calculate_data[ "local_config" ][ "is_use" ] == 1) {
                //查询本店的通讯地址
                $title = $calculate_data[ "local_config" ][ 'value' ][ 'local_name' ];
                if ($title == "") {
                    $title = '外卖配送';
                }
                $store_model = new Store();
                $store_condition = array (
                    [ 'site_id', '=', $data[ 'site_id' ] ],
                );
                if (addon_is_exit('store', $data[ 'site_id' ])) {
                    $store_condition[] = [ 'is_o2o', '=', 1 ];
                    $store_condition[] = [ 'status', '=', 1 ];
                    $store_condition[] = [ 'is_frozen', '=', 0 ];
                } else {
                    $store_condition[] = [ 'is_default', '=', 1 ];
                }
                $latlng = array (
                    'lat' => $data[ 'latitude' ],
                    'lng' => $data[ 'longitude' ],
                );
                $store_list = $store_model->getLocationStoreList($store_condition, '*', $latlng)[ 'data' ];
                $express_type[] = [ "title" => $title, "name" => "local", 'store_list' => $store_list ];
            }
//            }
        }
        $calculate_data[ "express_type" ] = $express_type;
        return $this->success($calculate_data);

    }

    /**
     * 计算
     * @param $data
     */
    public function calculate($data)
    {
        $site_id = $data[ 'site_id' ];
        $data = $this->initMemberAddress($data);
        $data = $this->initMemberAccount($data); //初始化会员账户

        //交易配置
        $config_model = new Config();
        $order_config = $config_model->getOrderEventTimeConfig($site_id)[ "data" ];
        $data[ 'order_config' ] = $order_config[ 'value' ] ?? [];

        $id = $data[ "id" ];
        $exchange_model = new Exchange();
        $exchange_info = $exchange_model->getExchangeInfo($id, '*', $data[ 'sku_id' ])[ "data" ];
        if (empty($exchange_info))
            return $this->error('', "找不到对应的积分兑换活动!");

        $data[ "exchange_info" ] = $exchange_info;
        if ($exchange_info[ "state" ] == 0) {
            $this->error = 1;
            $this->error_msg = "当前兑换活动未开启!";
        }

        if ($exchange_info[ "type" ] == 1 && $exchange_info[ "limit_num" ] > 0) {
            // 已兑换数量
            $exchangeed_num = model("promotion_exchange_order")->getSum([ [ 'exchange_id', '=', $exchange_info[ 'id' ] ], [ 'order_status', '<>', '-1' ], [ 'member_id', '=', $data[ 'member_id' ] ] ], 'num');
            if (( $exchangeed_num + $data[ 'num' ] ) > $exchange_info[ "limit_num" ]) {
                $this->error = 1;
                $this->error_msg = "兑换数量已超出该兑换活动的兑换限制!";
            }
        }

        if ($exchange_info[ "stock" ] <= 0) {
            if ($exchange_info[ 'type' ] == 2 && $exchange_info[ "stock" ] == -1) {
            } else {
                $this->error = 1;
                $this->error_msg = "当前兑换库存不足!";
            }
        }

        $delivery_status = 0;//发货状态
        $delivery_status_name = '';//发货状态名称
        $delivery_money = 0;//运费

        //兑换类型为1时   兑换物品为商品(相对优惠券和红包来说较为特殊)
        if ($exchange_info[ 'type' ] == 1) {
            $goods_model = new Goods();
            $goods_result = $goods_model->getGoodsSkuInfo([ [ 'sku_id', '=', $exchange_info[ 'sku_id' ] ], [ 'site_id', '=', $data[ 'site_id' ] ] ], '*');
            $goods_info = $goods_result[ 'data' ];
            $goods_info[ 'num' ] = $data[ 'num' ];
            $data[ 'goods_info' ] = $goods_info;
            $data[ 'is_virtual' ] = $goods_info[ 'is_virtual' ];

            //计算邮费
            if ($data[ 'is_virtual' ] == 1) {
                //虚拟订单  运费为0
                $delivery_money = 0;
                $data[ 'delivery' ][ 'delivery_type' ] = '';
                $data[ 'buyer_ask_delivery_time' ] = 0;
            } else {

                $express_config_model = new ExpressConfig();
                $deliver_type = $express_config_model->getDeliverTypeSort($site_id)[ "data" ];
                $data[ "deliver_sort" ] = explode(',', $deliver_type[ 'value' ][ 'deliver_type' ]);

                //查询店铺是否开启快递配送
                $data[ "express_config" ] = $express_config_model->getExpressConfig($site_id)[ "data" ];

                //查询店铺是否开启门店自提
                $data[ "store_config" ] = $express_config_model->getStoreConfig($site_id)[ "data" ];

                //查询店铺是否开启外卖配送
                $data[ "local_config" ] = $express_config_model->getLocalDeliveryConfig($site_id)[ "data" ];

                //如果本地配送开启, 则查询出本地配送的配置
                if ($data[ "local_config" ][ 'is_use' ] == 1 && isset($data[ 'delivery' ][ 'store_id' ])) {
                    $local_model = new Local();
                    $local_info = $local_model->getLocalInfo([ [ 'site_id', '=', $site_id ], [ 'store_id', '=', $data[ 'delivery' ][ 'store_id' ] ] ])[ 'data' ];
                    $data[ "local_config" ][ 'info' ] = $local_info;
                } else {
                    $shop_goods[ 'local_config' ][ 'info' ] = [];
                }

                $delivery_array = $data[ 'delivery' ] ?? [];
                $delivery_type = $delivery_array[ "delivery_type" ] ?? 'express';
                $data[ 'buyer_ask_delivery_time' ] = $delivery_type != 'express' ? $data[ 'buyer_ask_delivery_time' ] : 0;

                if ($delivery_type == "store") {

                    if (isset($data[ 'delivery' ][ "delivery_type" ]) && $data[ 'delivery' ][ "delivery_type" ] == "store") {
                        //门店自提
                        $delivery_money = 0;
                        $data[ 'delivery' ][ 'delivery_type' ] = 'store';
                        if ($data[ "store_config" ][ "is_use" ] == 0) {
                            $this->error = 1;
                            $this->error_msg = "门店自提方式未开启!";
                        }
                        if (empty($data[ 'delivery' ][ "store_id" ])) {
                            $this->error = 1;
                            $this->error_msg = "门店未选择!";
                        }
                        $data[ 'delivery' ][ 'store_id' ] = $data[ 'delivery' ][ "store_id" ];
                        $shop_goods[ 'buyer_ask_delivery_time' ] = $data[ 'buyer_ask_delivery_time' ];
                        $data = $this->storeOrderData($data);
                    }
                } else {
                    if (empty($data[ 'member_address' ]) && $delivery_type == 'express') {
                        $delivery_money = 0;
                        $data[ 'delivery' ][ 'delivery_type' ] = 'express';

                        $this->error = 1;
                        $this->error_msg = "未配置默认收货地址!";
                    } else {
                        if (!isset($data[ 'delivery' ][ "delivery_type" ]) || $data[ 'delivery' ][ "delivery_type" ] == "express") {
                            if ($data[ "express_config" ][ "is_use" ] == 1) {

                                if ($exchange_info[ 'is_free_shipping' ] == 1) {
                                    //免邮
                                    $delivery_money = 0;
                                } elseif ($exchange_info[ 'is_free_shipping' ] == 0 && $exchange_info[ 'delivery_type' ] == 0) {
                                    //固定运费
                                    $delivery_money = $exchange_info[ 'delivery_price' ];
                                } else {

                                    if ($exchange_info[ 'delivery_type' ] == 1) {
                                        //运费模板
                                        $goods_info[ 'shipping_template' ] = $exchange_info[ 'shipping_template' ];
                                    }
                                    //物流配送
                                    $express = new Express();
                                    $express_fee_result = $express->pointExchangeCalculate($goods_info, $data);
                                    if ($express_fee_result[ "code" ] < 0) {
                                        $this->error = 1;
                                        $this->error_msg = $express_fee_result[ "message" ];
                                        $delivery_money = 0;
                                    } else {
                                        $delivery_money = $express_fee_result[ 'data' ][ 'delivery_fee' ];
                                    }
                                }

                            } else {
                                $this->error = 1;
                                $this->error_msg = "物流配送方式未开启!";
                                $delivery_money = 0;
                            }
                            $data[ 'delivery' ][ 'delivery_type' ] = 'express';
                        } else if ($data[ 'delivery' ][ "delivery_type" ] == "local") {

                            //外卖配送
                            $delivery_money = 0;
                            $data[ 'delivery' ][ 'delivery_type' ] = 'local';
                            if ($data[ "local_config" ][ "is_use" ] == 0) {
                                $this->error = 1;
                                $this->error_msg = "外卖配送方式未开启!";
                            } else {
                                if (empty($data[ 'delivery' ][ "store_id" ])) {
                                    $this->error = 1;
                                    $this->error_msg = "没有可配送的门店!";
                                }
                                $local_delivery_time = 0;
                                if (!empty($data[ 'buyer_ask_delivery_time' ])) {
                                    $local_delivery_time = $data[ 'buyer_ask_delivery_time' ];
                                }
                                $goods_info[ 'buyer_ask_delivery_time' ] = $local_delivery_time;

                                $local_model = new Local();
                                $local_result = $local_model->pointExchangeCalculate($goods_info, $data);
                                $data[ 'delivery' ][ 'start_money' ] = 0;
                                if ($local_result[ 'code' ] < 0) {
                                    $this->error = $local_result[ 'data' ][ 'code' ];
                                    $this->error_msg = $local_result[ 'message' ];
                                    $data[ 'delivery' ][ 'start_money' ] = $local_result[ 'data' ][ 'start_money_array' ][ 0 ] ?? 0;
                                } else {
                                    $delivery_money = $local_result[ 'data' ][ 'delivery_money' ];
                                    if (!empty($local_result[ 'data' ][ 'error_code' ])) {
                                        $this->error = $local_result[ 'data' ][ 'code' ];
                                        $this->error_msg = $local_result[ 'data' ][ 'error' ];
                                    }
                                }

                                $data[ 'delivery' ][ 'error' ] = $this->error;
                                $data[ 'delivery' ][ 'error_msg' ] = $this->error_msg;

                            }

                        }
                    }
                }
            }
        }

        $data[ 'delivery_status' ] = $delivery_status;
        $data[ 'delivery_status_name' ] = $delivery_status_name;
        $point = $exchange_info[ "point" ];
        $price = $exchange_info[ "price" ];
        $balance = $exchange_info[ "balance" ];
        $goods_num = $data[ "num" ];

        $goods_money = $price * $data[ "num" ];

        $order_money = $goods_money + $delivery_money;

        $order_name = $exchange_info[ "name" ] . "【" . $exchange_info[ "type_name" ] . "】";
        $data[ 'point' ] = $point * $data[ "num" ];
        $data[ 'price' ] = $price * $data[ "num" ];
        $data[ 'goods_num' ] = $goods_num;
        $data[ 'order_name' ] = $order_name;
        $data[ 'order_money' ] = $order_money;
        $data[ 'balance' ] = $balance * $data[ "num" ];
        $data[ 'delivery_money' ] = $delivery_money;

        return $data;
    }

    /**
     * 初始化收货地址
     * @param $data
     * @return mixed
     */
    public function initMemberAddress($data)
    {

        $delivery_type = $data[ 'delivery' ][ 'delivery_type' ] ?? '';
        if (empty($data[ 'member_address' ])) {
            $member_address = new MemberAddress();
            $type = 1;
            if ($delivery_type == 'local') {
                $type = 2;
            }
            $address = $member_address->getMemberAddressInfo([ [ 'member_id', '=', $data[ 'member_id' ] ], [ 'is_default', '=', 1 ], [ 'type', '=', $type ] ]);
            $data[ 'member_address' ] = $address[ 'data' ];
        }

        if (!empty($data[ 'member_address' ])) {
            if ($delivery_type == 'local') {
                //外卖订单 如果收货地址没有定位的话,就不取用地址
                $type = $data[ 'member_address' ][ 'type' ] ?? 1;
                if ($type == 1) {
                    $data[ 'member_address' ] = '';
                }
            }
        }

        return $data;
    }

    /**
     * 初始化会员账户
     * @param $data
     * @return mixed
     */
    public function initMemberAccount($data)
    {
        $member_model = new Member();
        $member_info_result = $member_model->getMemberDetail($data[ "member_id" ], $data[ "site_id" ]);
        $member_info = $member_info_result[ "data" ];
        $data[ 'member_account' ] = $member_info;
        return $data;
    }

    /**
     * 增加订单自动关闭事件
     * @param $order_id
     * @param $site_id
     */
    public function addOrderCronClose($order_id, $site_id)
    {
        //计算订单自动关闭时间
        $config_model = new Config();
        $order_config_result = $config_model->getOrderEventTimeConfig($site_id);
        $order_config = $order_config_result[ "data" ];
        $now_time = time();
        if (!empty($order_config)) {
            $execute_time = $now_time + $order_config[ "value" ][ "auto_close" ] * 60;//自动关闭时间
        } else {
            $execute_time = $now_time + 3600;//尚未配置  默认一天
        }
        $cron_model = new Cron();
        $cron_model->addCron(1, 0, "积分兑换订单自动关闭", "CronExchangeOrderClose", $execute_time, $order_id);
    }

    /**
     * 验证订单支付金额知否为0  如果为0  立即支付完成
     * @param $order_data
     */
    public function checkFree($order_data)
    {
        if ($order_data[ "price" ] == 0) {
            $pay_model = new Pay();
            $pay_model->onlinePay($order_data[ "out_trade_no" ], "ONLINE_PAY", '', '');
        }

    }

    /**
     * 生成订单编号
     *
     * @param array $site_id
     */
    public function createOrderNo($site_id, $member_id = 0)
    {
        $time_str = date('YmdHi');
        $max_no = Cache::get($site_id . "_" . $member_id . "_" . $time_str);
        if (!isset($max_no) || empty($max_no)) {
            $max_no = 1;
        } else {
            $max_no = $max_no + 1;
        }
        $order_no = $time_str . $member_id . sprintf("%03d", $max_no);
        Cache::set($site_id . "_" . $member_id . "_" . $time_str, $max_no);
        return $order_no;
    }

    private function orderType($type_name)
    {
        if ($type_name == 'express') {
            $order = new CommonOrder();
            return [
                'order_type_id' => 1,
                'order_type_name' => '普通订单',
                'order_status' => $order->order_status[ 0 ]
            ];
        } elseif ($type_name == 'store') {
            $order = new StoreOrder();
            return [
                'order_type_id' => 2,
                'order_type_name' => '自提订单',
                'order_status' => $order->order_status[ 0 ]
            ];
        } elseif ($type_name == 'local') {
            $order = new LocalOrder();
            return [
                'order_type_id' => 3,
                'order_type_name' => '外卖订单',
                'order_status' => $order->order_status[ 0 ]
            ];
        }
    }

    /**
     * 补齐门店数据
     * @param $data
     */
    public function storeOrderData($shop_goods)
    {
        $temp_data = [];
        $delivery_store_id = $shop_goods[ 'delivery' ][ 'store_id' ] ?? 0; //门店id

        if ($delivery_store_id > 0) {
            $store_model = new Store();
            $condition = array (
                [ "store_id", "=", $delivery_store_id ],
                [ "site_id", "=", $shop_goods[ 'site_id' ] ],
                [ "status", "=", 1 ],
                [ "is_pickup", "=", 1 ],
            );
            $store_info_result = $store_model->getStoreInfo($condition);
            $store_info = $store_info_result[ "data" ] ?? [];
            if (empty($store_info)) {
                $this->error = 1;
                $this->error_msg = "当前门店不存在或未开启!";
            } else {
                $temp_data[ "delivery_store_id" ] = $delivery_store_id;
                $delivery_store_name = $store_info_result[ "data" ][ "store_name" ];
                $temp_data[ "delivery_store_name" ] = $delivery_store_name;
                $store_info[ 'full_address' ] = $store_info[ "full_address" ] . $store_info[ "address" ];
                $temp_data[ "delivery_store_info" ] = json_encode($store_info, JSON_UNESCAPED_UNICODE);
            }
        } else {
            $this->error = 1;
            $this->error_msg = "配送门店不可为空!";
        }
        return array_merge($shop_goods, $temp_data);
    }
}